<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
 Admin模块控制器
 因为权限所需所以重新定义
 */
namespace temmoku;
use \temmoku\controller;
use \temmoku\lib\cookie;
abstract class admin extends controller{
	function __construct()
    {
    	/* 
    	 *主后台必须调用此控制器，以便控制权限
    	 */
    	parent :: __construct();
		$ADMIN_POWER=include_once APP_PATH.'power.php';
		C($ADMIN_POWER);
		$weburl = C('adminurl') ? HTTP.C('adminurl').'/' :WEBURL;
		define('ADMIN_WEBURL', $weburl);
		//主后台视图位置常量
		define('ADMIN_VIEW', "/admin/view/");
		hook_listen('global_admin_begin');
		if(is_rorced_admin==false){
			$this->Admin_Check();
			//模型上传插件判断入口
			define('from_upfile_custiom_model', "admin");
		}
		hook_listen('global_admin_end');
    }
    //进行管理后台登陆检查
    private function Admin_Check(){
    	
		$admin_login_ip=C('admin_login_ip');
		if($admin_login_ip){
			$LOGIN_IP=explode(PHP_EOL,$admin_login_ip);
			$LOGIN_IP=array_map('get_iplong',$LOGIN_IP);
			if(!in_array(get_iplong(onlineip),$LOGIN_IP)){
				$this->err('你所在的IP禁止登陆后台');
			}
		}
		$ORDINARY_ADMIN=cookie::get('ORDINARY_ADMIN');
		//如果存在则验证
		if($ORDINARY_ADMIN){
			$_ORDINARY_ADMIN=decode($ORDINARY_ADMIN);
			list($uid,$password,$onlineip) =explode(' ',$_ORDINARY_ADMIN);
			$uid=intval($uid);
			if(!$uid || !$password || C('ONLINEIP') != $onlineip || !$onlineip){
				Cookie::del('ORDINARY_ADMIN');
				$this->Success('验证信息不符合，需要重新登录',ADMIN_WEBURL.'/admin/index/login','10');
			}
			
			$data = (new lib\user)->Cookie_Login($uid,$password);
			if('0'===$data['code']){
				C('MYDB',$data['UserDB']);
				$MYGROUP=C('USER_GROUP.'.$data['UserDB']['groupid']);
				$MYGROUP['setting']=unserialize($MYGROUP['setting']);
				C('MYGROUP',$MYGROUP);
				if(3 !==$data['UserDB']['groupid']){
					//检查是否有权限登陆后台，并且进行操作
					$this->Admin_Permission_Check();
				}
				//将编辑器设置加载进来
       			if(	!C('UEDITOR_CONFIG.function')){
					$file_conf=APP_PATH."plugin/ueditor/conf/";
					load_conf($file_conf);
					C('UEDITOR_CONFIG.function',unserialize(C('ueditor|UEDITOR_CONFIG.function')));
				}
				$this->user=$data['UserDB'];
			}else{
				Cookie::del('ORDINARY_ADMIN');
				$this->Success($data['content'],ADMIN_WEBURL.'/admin/index/login','10');
			}
		}else{
			if('login' !== METHOD){
				$this->Success('您还没有登陆,正在为您跳转到登陆页面',ADMIN_WEBURL.'/admin/index/login','10');
				exit;
			}
		}
    }
    public function Admin_Permission_Check($type=false){
    
    	if(1===c('MYGROUP.if_login_admin')){
    		$setting=C('MYGROUP.setting');
    		!isset($setting['power'][CONTROLLER] ) && $setting['power'][CONTROLLER]=[];
    		if(in_array(METHOD ,$setting['power'][CONTROLLER])){
    			//如果为真则cookie,登陆的时候才使用
    			if($type){
    				$ORDINARY_ADMIN=encode(C('MYDB.uid').' '.C('MYDB.password').' '.C('ONLINEIP'));
					//写入浏览器缓存
					Cookie::set('ORDINARY_ADMIN',$ORDINARY_ADMIN);
					$this->success('登陆成功，正在为您跳转',ADMIN_WEBURL.'/admin',100);
    			}
    		}else{
    			$this->err('您没有权限操作');
    		}
    	}else{
    		$this->err('您没有权限登陆后台');
    	}
    }
}

?>
